﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web;
using Telerik.Web.UI;

namespace portfolio.Views
{
    public partial class ContactInfo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool messageSent = false;
            messageSent = SendMail();
            ClearContactInfo();
            if (messageSent)
            {
                rwmMessageSent.RadAlert("Your message has been sent. I will respond as soon as possible.", 330, 180, "Message Sent", "");
            }
        }

        private bool SendMail()
        {
            bool messageSent = false;
            if (txtContactName.Text == string.Empty || txtBody.Text == string.Empty || (txtEmail.Text == string.Empty && txtPhoneNumber.Text == string.Empty))
            {
                RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(String.Format("$find('{0}').show();", rwRequiredFields.ClientID));
            }
            else
            {
                MailMessage mailMessage = new MailMessage();
                SmtpClient smtpClient = new SmtpClient();
                NetworkCredential credentials = new NetworkCredential();
                string body = "";

                mailMessage.From = new MailAddress("virgil@virgilbreeden.com");
                mailMessage.To.Add(new MailAddress("virgil.breeden@gmail.com"));
                mailMessage.Subject = txtSubject.Text;

                body = txtBody.Text + "\n\n";
                body += "Contact Name: " + txtContactName.Text + "\n\n";
                body += "Company Name: " + txtCompanyName.Text + "\n\n";
                body += "Phone: " + txtPhoneNumber.Text + "\n\n";
                body += "Email: " + txtEmail.Text + "\n\n";

                mailMessage.Body = body;

                smtpClient.Host = "mail.virgilbreeden.com";
                smtpClient.Port = 25;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                credentials = new NetworkCredential("virgil@virgilbreeden.com", "db4fqz9b??");
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = credentials;

                smtpClient.Send(mailMessage);

                messageSent = true;
            }

            return messageSent;
        }

        private void ClearContactInfo()
        {
            txtContactName.Text = string.Empty;
            txtCompanyName.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtPhoneNumber.Text = string.Empty;
            txtSubject.Text = string.Empty;
            txtBody.Text = string.Empty;
        }
    }
}